import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:study_demo/base/BaseController.dart';
import 'package:study_demo/util/utils.dart';

import '../../../api/api.dart';

class PhoneNumQueryController extends BaseController {
  final result = ''.obs;

  TextEditingController _phoneNumEditingController = TextEditingController();

  TextEditingController get phoneNumEditingController =>
      _phoneNumEditingController;

  @override
  onInit() {
    super.onInit();
    query();
  }

  query() async {
    String phone = _phoneNumEditingController.text.toString();
    if (phone.isEmpty) {
      toast('手机号码不能为空');
      return;
    }
    Dio dio = Dio();
    var response = await dio.request(phoneNumQueryUrl + phone);
    if (response.statusCode == 200) {
      var data = jsonDecode(response.data)['data'];
      print('data:$data');
      result.value = data['province'] + ' ' + data['city'] + ' ' + data['sp'];
    }
  }

  clear() {
    _phoneNumEditingController.text = '';
  }

  copy() {
    copyToClipboard(result.value);
  }
}
